
package com.mp.chart.renderer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

import com.mp.chart.charts.BarLineChartBase;
import com.mp.chart.components.AJXAxis;
import com.mp.chart.utils.Transformer;
import com.mp.chart.utils.Utils;
import com.mp.chart.utils.ViewPortHandler;

public class AJXAxisRenderer extends XAxisRenderer {

    private final BarLineChartBase mChart;
    protected AJXAxis mXAxis;

    public AJXAxisRenderer(BarLineChartBase chart, ViewPortHandler viewPortHandler, AJXAxis xAxis, Transformer trans) {
        super(viewPortHandler, xAxis, trans);
        mXAxis = xAxis;
        mChart = chart;
    }


    /**
     * draws the x-labels on the specified y-position
     *
     * @param pos
     */
    protected void drawLabels(Canvas c, float pos) {

        // pre allocate to save performance (dont allocate in loop)
        float[] position = new float[]{
                0f, 0f
        };


        if (mXAxis.getShowLabels() == null) {
            throw new RuntimeException("必须调用XAxis.setShowLabels()方法");
        }

        int count = mXAxis.getShowLabels().size();

        float perWidth = 0;// mViewPortHandler.contentWidth() / count;
        TextPaint tp = new TextPaint(mAxisLabelPaint);
        tp.setTextAlign(Paint.Align.LEFT);
/*有瑕疵*/
        if (mXAxis.getXlabelStatic()) {
            int startXIndex = mChart.getLowestVisibleXIndex();
            int stopXIndex = mChart.getHighestVisibleXIndex();
            int avage = (stopXIndex - startXIndex) / 5;
            for (int i = 0; i <= 5; i++) {
                String label = mXAxis.getShowLabels().valueAt(startXIndex + i * avage);
                if (mXAxis.getValueFormatter() != null) {
                    label = mXAxis.getValueFormatter().getFormattedValue(label);
                }
                position[0] = mViewPortHandler.contentLeft() + i * (mViewPortHandler.contentRight() - mViewPortHandler.contentLeft()) / 5;
                c.drawText(label, position[0],
                        pos,
                        mAxisLabelPaint);

            }
        } else {
            for (int i = 0; i < count; i += mXAxis.mAxisLabelModulus) {
                int ix = mXAxis.getShowLabels().keyAt(i);

                position[0] = ix;

                mTrans.pointValuesToPixel(position);

                if (mViewPortHandler.isInBoundsX(position[0])) {

                    String label = mXAxis.getShowLabels().valueAt(i);

                    if (mXAxis.getValueFormatter() != null) {
                        label = mXAxis.getValueFormatter().getFormattedValue(label);
                    }

                    if (mXAxis.getTextPosition() == AJXAxis.TextPosition_CENTER) {
                        if (perWidth == 0) {
                            perWidth = position[0] - mViewPortHandler.contentLeft();
                        }
                        c.save();
                        StaticLayout staticLayout = new StaticLayout(label, tp, (int) perWidth, Layout.Alignment.ALIGN_CENTER, 1, 0, true);
                        c.translate(mViewPortHandler.contentLeft() + (perWidth) * i, pos - staticLayout.getHeight());
                        staticLayout.draw(c);
                        c.restore();
                    } else if (mXAxis.getTextPosition() == AJXAxis.TextPosition_Middle) {//五日 与普通区别在于轴index记录的是左边,普通的靠右,故普通相减
                         perWidth =  mViewPortHandler.contentWidth() / count;
                        c.save();
                        StaticLayout staticLayout = new StaticLayout(label, tp, (int) perWidth, Layout.Alignment.ALIGN_CENTER, 1, 0, true);
                        c.translate(mViewPortHandler.contentLeft() + (perWidth) * i, pos - staticLayout.getHeight());
                        staticLayout.draw(c);
                        c.restore();

                    } else {
                        int labelWidth = Utils.calcTextWidth(mAxisLabelPaint, label);
                        if ((labelWidth / 2 + position[0]) > mChart.getViewPortHandler().contentRight()) {
                            position[0] = mChart.getViewPortHandler().contentRight() - labelWidth / 2;
                        } else if ((position[0] - labelWidth / 2) < mChart.getViewPortHandler().contentLeft()) {
                            position[0] = mChart.getViewPortHandler().contentLeft() + labelWidth / 2;
                        }
                        c.drawText(label, position[0],
                                pos,
                                mAxisLabelPaint);
                    }

                }
            }
        }


//
    }

    protected Path mRenderGridLinesPath = new Path();

    /*x轴垂直线*/
    @Override
    public void renderGridLines(Canvas c) {

        try {
            if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
                return;

            // pre alloc
            float[] position = new float[]{
                    0f, 0f
            };

            mGridPaint.setColor(mXAxis.getGridColor());
            mGridPaint.setStrokeWidth(mXAxis.getGridLineWidth());
            mGridPaint.setPathEffect(mXAxis.getGridDashPathEffect());

            // Path gridLinePath = new Path();
            Path gridLinePath = mRenderGridLinesPath;
            gridLinePath.reset();
            if (mXAxis.getShowLabels() == null) {
                throw new RuntimeException("必须调用XAxis.setShowLabels()方法");
            }

            int count = mXAxis.getShowLabels().size();

//        System.out.println("mViewPortHandler:" + mTrans.getValueMatrix());


            if (!mChart.isScaleXEnabled()) {
                count -= 1;
            }
            if (mXAxis.getXlabelStatic()) {
                float startX = mViewPortHandler.contentLeft();
                float stopX = mViewPortHandler.contentRight();
                float avage = (stopX - startX) / 5;
                for (int i = 0; i < 5; i++) {
                    gridLinePath.moveTo(startX + i * avage, mViewPortHandler.contentBottom());
                    gridLinePath.lineTo(startX + i * avage, mViewPortHandler.contentTop());
                    c.drawPath(gridLinePath, mGridPaint);
                }
                gridLinePath.reset();
            } else {
                for (int i = 0; i <= count; i += mXAxis.mAxisLabelModulus) {

                    String t = mXAxis.getShowLabels().valueAt(i);

                    int ix = mXAxis.getShowLabels().keyAt(i);

                    position[0] = ix;
                    mTrans.pointValuesToPixel(position);


                    if (position[0] >= mViewPortHandler.offsetLeft()
                            && position[0] <= mViewPortHandler.getChartWidth()) {

                        gridLinePath.moveTo(position[0], mViewPortHandler.contentBottom());
                        gridLinePath.lineTo(position[0], mViewPortHandler.contentTop());

                        // draw a path because lines don't support dashing on lower android versions
                        c.drawPath(gridLinePath, mGridPaint);
                    }

                    gridLinePath.reset();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
